package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.TimeMarksMorePicActivity;
import com.github.postsanf.yinian.activity.pop.PopSetTimeMarksBg;
import com.github.postsanf.yinian.bean.TimeMarksCoverBean;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/27.
 */
public class TimeMarksCoverAdapter extends RecyclerView.Adapter<TimeMarksCoverAdapter.ViewHolder>{

    private Context context;
    private List<TimeMarksCoverBean.DataBean> mDatas;
    private List<String> showDatas=new ArrayList<>();
    private int selectedPosotion;
    private PopSetTimeMarksBg mpopSetAvatar;

    public TimeMarksCoverAdapter(Context context, List<TimeMarksCoverBean.DataBean>  mDatas){
        this.context=context;
        this.mDatas=mDatas;
        showDatas.add("本地图片");
        for (int i = 0; i <6; i++) {
            showDatas.add(mDatas.get(i).getOfficialCoverPicture());
            mDatas.remove(mDatas.get(i).getOfficialCoverPicture());
        }
        showDatas.add("更多图片");
        mpopSetAvatar = new PopSetTimeMarksBg((Activity) context, itemsOnClick, itemsOnTouch);
    }

    public class ViewHolder extends  RecyclerView.ViewHolder{

        ImageView item_bg;
        ImageView iv_local_select;
        ImageView iv_selected;
        SimpleDraweeView sd_cover;

        public ViewHolder(View itemView) {
            super(itemView);
            sd_cover = ((SimpleDraweeView) itemView.findViewById(R.id.sd_cover));
            iv_local_select = ((ImageView) itemView.findViewById(R.id.iv_local_select));
            iv_selected = ((ImageView) itemView.findViewById(R.id.iv_selected));
            item_bg = ((ImageView) itemView.findViewById(R.id.item_bg));
        }

        public void bindData(final String coverUrl, final int position){
            if (TextUtils.equals(coverUrl,"本地图片")&&position==0){
                iv_local_select.setVisibility(View.VISIBLE);
                sd_cover.setVisibility(View.GONE);
                iv_selected.setVisibility(View.GONE);
                item_bg.setVisibility(View.GONE);
                iv_local_select.setImageResource(R.drawable.local_select);
                iv_local_select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mpopSetAvatar.showAtLocation(iv_local_select, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                        ((TextView) mpopSetAvatar.mMenuView.findViewById(R.id.btn_alert_avatar_title)).setVisibility(View.GONE);
                    }
                });
            }else if(TextUtils.equals(coverUrl,"更多图片")&&position==7){
                iv_selected.setVisibility(View.GONE);
                iv_local_select.setVisibility(View.VISIBLE);
                sd_cover.setVisibility(View.GONE);
                item_bg.setVisibility(View.GONE);
                iv_local_select.setImageResource(R.drawable.more_pic);
                iv_local_select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        Intent intent = new Intent(context, TimeMarksMorePicActivity.class);
//                        Bundle bundle = new Bundle();
//                        bundle.putSerializable("CoverPic",mDatas);
                        List<String> strData=new ArrayList<String>();
                        for (int i = 0; i <mDatas.size() ; i++) {
                            strData.add(mDatas.get(i).getOfficialCoverPicture());
                        }
                        context.startActivity(new Intent(context,TimeMarksMorePicActivity.class).putStringArrayListExtra("CoverPic", (ArrayList<String>) strData));
                    }
                });
            }else{
                iv_local_select.setVisibility(View.GONE);
                sd_cover.setVisibility(View.VISIBLE);
                ImageOptHelper.showFrescoImg(Uri.parse(coverUrl),sd_cover,context);
                if (selectedPosotion==position){
                    iv_selected.setVisibility(View.VISIBLE);
                    item_bg.setVisibility(View.VISIBLE);
                    item_bg.setAlpha(0.5f);
                }else{
                    iv_selected.setVisibility(View.GONE);
                    item_bg.setVisibility(View.GONE);
                }
                sd_cover.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener!=null){
//                            Log.d("yuhao", "coverUrl="+coverUrl);
                            listener.setOnPicChangeListener(coverUrl,position);
                        }
                        selectedPosotion=position;
                        iv_selected.setVisibility(View.VISIBLE);
                        item_bg.setVisibility(View.VISIBLE);
                        item_bg.setAlpha(0.5f);
                    }
                });
            }
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView=LayoutInflater.from(context).inflate(R.layout.time_marks_cover_item,parent,false);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
//        Log.d("yuhao", "showDatas="+showDatas.toString());
        String convertView = showDatas.get(position);
        holder.bindData(convertView,position);
    }

    @Override
    public int getItemCount() {
        return mDatas==null? 1:8;
    }

    private OnPicChangeListener listener;
    public interface OnPicChangeListener{
        void setOnPicChangeListener(String url, int position);
    }

    public void setOnPicChangeListener(OnPicChangeListener listener){
        this.listener=listener;
    }

    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        public void onClick(View v) {
            mpopSetAvatar.dismiss();
            switch (v.getId()) {
                case R.id.btn_setuser_take_photo:
                    ImageUtils.pickImageFromCamera((Activity) context);
                    break;
                case R.id.btn_setuser_pick_photo:
                    ImageUtils.pickImageFromAlbum((Activity) context);
                    break;
                case R.id.btn_setuser_cancel_photo:
                    break;
            }
        }
    };

    private View.OnTouchListener itemsOnTouch = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int height = mpopSetAvatar.mMenuView.findViewById(R.id.pop_setuser_advatar_layout).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    mpopSetAvatar.dismiss();
                }
            }
            return true;
        }
    };
}
